package com.test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.UnsupportedEncodingException;

import javax.swing.JPanel;

public class StartPanel extends JPanel implements Runnable,KeyListener{
	

	private static final long serialVersionUID = 1L;
	private Font myFont;
	int times=0;
	String str="";
	String pret="";
	private MyTankGame jf=null;
	private String s_iso88591;
	
	public void paint(Graphics g)
	{
		
		
		super.paint(g);
		g.fillRect(0,0,600,400);
		//notification
		if(times%2==0){
		g.setColor(Color.yellow);
		
		setMyFont(new Font("Verdana",Font.BOLD,30));
		g.setFont(myFont);
		g.drawString(str,160,180);
		setMyFont(new Font("Arial",Font.ITALIC,20));
		g.setFont(myFont);
		g.drawString(pret, 330, 360);
		}
		
	}

	public Font getMyFont() {
		return myFont;
	}

	public void setMyFont(Font myFont) {
		this.myFont = myFont;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		while(true)
		{
			try{
				Thread.sleep(500);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			times++;
			if(jf.getLanguage()==0) {
				str="Vous êtes prêt ?";
			}else if(jf.getLanguage()==1) {
				str ="Are you ready ?";
			}else if (jf.getLanguage()==2) {
				try {
					s_iso88591 = new String("你好??".getBytes("GBK"),"iso-8859-1");
					str=new String(s_iso88591.getBytes("iso-8859-1"),"GBK");
					

				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				
				
				
			}
				
		
			this.repaint();
			if(times >= 5){
				if(jf.getLanguage()==0) {
					str = "Allooooooons-y!!";
				}else {
					str ="Gooooooooooooo!!";
				}
				
				
			}
			if(times == 6){
				pret = "Press any key to continue...";
				break;
			}
		}
		
		
	}

	
	public MyTankGame getJf() {
		return jf;
	}

	public void setJf(MyTankGame jf) {
		this.jf = jf;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(times==6){
		//on construit le panel du jeu en supprimant le panel d'accueil
				
				this.getJf().remove(this);
				this.getJf().removeKeyListener(this);
				
				MyPanel mp = new MyPanel(0);
				this.getJf().add(mp);
				this.getJf().addKeyListener(mp);
				this.getJf().setMp(mp);
				this.getJf().jmi3.setEnabled(true);
				
				
				//actualiser JFrame
				this.getJf().invalidate();
				this.getJf().repaint();
				this.getJf().setVisible(true);
				mp.setJf(this.getJf());
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}


